<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <script type="text/javascript" src="resources/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="resources/js/jquery-ui-1.8.18.min.js"></script>
    <script type="text/javascript" src="resources/js/jquery.json-2.3.min.js"></script>
    <!--link rel="stylesheet" type="text/css" href="resources/style.css" /-->
    <link rel="stylesheet" type="text/css" href="resources/styleNew.css" />
    <title>Webový repozitář pro vývojové artefakty</title>
    
</head>
<body>
<div id="header">
    <div id="header_left"></div>
    <a id="logo"></a>
    <div id="header_right"></div>
    <div id="menuTop">
        <ul>
            <li id="butSec1" class="active"><a>Repository</a></li>
            <li id="butSec2"><a >Team &amp; duties</a></li>
        </ul>
    </div>
</div>

<div id="content_left">
<div id="content_right">
<div id="content_help">
<div id="content">
&nbsp;<br />
<div id="section1">
    <h2>Repository</h2>

    <div class="center">
    <a id="up"><img src='resources/images/new.png' alt='add' /> Upload development artifact</a>
    <div id="upform" class="pop centerT">
        <form:form method="post" action="save.html" modelAttribute="fileObject"
                enctype="multipart/form-data">
                <input name="file" type="file" />
                <input type="submit" class="button" value="Upload" />
        </form:form>
    </div>
    </div>
    <div id="menu">
    <table class="centerT"><tr>
    <!--td id="up">Upload</td-->
    <td id="war"><input type="button" class="button" value="WAR"/></td>
    <td id="xsd"><input type="button" class="button" value="XSD"/></td>
    <td id="wsdl"><input type="button" class="button" value="WSDL"/></td>
    <td id="hidden"><input type="button" class="button" value="Search" id="searchButt" disabled="disabled"/></td>
    </tr></table>
    </div>
    <div id="search" class="pop">
		<form id="searchformId" class="searchform" method="POST">
                <label for="name">Name</label><input name="name" type="text" /><br/>
                <div class="fwar"><label for="listener">Listener</label><input name="listener" type="text" class="fwar"/><br/>
                <label for="filter">Filter</label><input name="filter" type="text" class="fwar"/><br/></div>
                <div class="fwsdl"><label for="operation">Operation</label><input name="operation" type="text" /><br/>
                <label for="response">Response</label><input name="response" type="text" class="fwsdl"/><br/>
                <label for="request">Request</label><input name="request" type="text" class="fwsdl"/><br/></div>
                <div class="fxsd"><label for="simpleType">Simple Type</label><input name="simpleType" type="text" class="fxsd"/><br/>
                <label for="complexType">Complex Type</label><input name="complexType" type="text" class="fxsd"/><br/>
                <label for="attribute">Attribute</label><input name="attribute" type="text" class="fxsd"/><br/>
                <label for="element">Element</label><input name="element" type="text" /><br/></div>
                <input name="type" type="hidden" value="" />
                <input type="submit" id="searchbutton" class="button" value="Search"/>
        </form>
    </div>
    <div id="list" class="pop">
    </div>
    <div id="info" class="pop2">
    	<div id="paging"></div>
    	<div id="versionInfo"></div>
    </div>
</div>

<div class="hiddenC" id="section2">
    <h2>Team &amp; duties</h2>
    <ul>
        <li>Pavel Synek (<a href="http://is.muni.cz/auth/osoba/396020" rel="nofollow">396020</a>)
        <ul><li>Data processing and working with XML database </li></ul></li>
        <li>Martin Laštovička  (<a href="http://is.muni.cz/auth/osoba/395855" rel="nofollow">395855</a>)
        <ul><li>Data processing and working with XML database </li></ul></li>
        <li>Anna Sakařová (<a href="http://is.muni.cz/auth/osoba/396021" rel="nofollow">396021</a>)
        <ul><li>Front end and server core </li></ul></li>
        <li>Jiří Klimeš (<a href="http://is.muni.cz/auth/osoba/396126" rel="nofollow">396126</a>)
        <ul><li>Front end and XML specification </li></ul></li>
    </ul>
</div>

</div>
</div>
</div>
</div>
<div id="bottom">
  <div id="bottom_left"></div>
  <div id="copyright">&copy; 2013 Pavel Synek, Martin Laštovička, Anna Sakařová, Jiří Klimeš</div>
  <div id="bottom_right"></div>
</div>
	
<script type="text/javascript">
$(document).ready(function(){
	
	//------menu functions---------------
        $("#logo").click(function() {
		$("#section1").show();
		$(".active").removeClass("active");
       	$("#butSec1").addClass("active");
		$("#section2").hide();
		$("#section3").hide();
	});
        $("#butSec1").click(function() {
    	$(".active").removeClass("active");
       	$(this).addClass("active");
		$("#section1").show();
		$("#section2").hide();
	});
        $("#butSec2").click(function() {
    	$(".active").removeClass("active");
       	$(this).addClass("active");
		$("#section1").hide();
		$("#section2").show();
	});
	$("#up").click(function() {
        if ($("#upform").is(":hidden")) {
            $("#upform").slideDown("slow");
        } else {
            $("#upform").slideUp("slow");
        }
		/*$("#upform").toggle();*/
	});
	$("#war").click(function() {
		//$("#list").toggle();
		//$("#hidden").toggle();
		//$("#list").toggleClass("war");
		$("#searchButt").removeAttr("disabled");
		getAll("war");
		$(".fwar").css("display", "inline");
		$(".fwsdl").css("display", "none");
		$(".fxsd").css("display", "none");
		$(".searchform [name=type]").val("war");
		$("#info").slideUp("slow");
	});
	$("#xsd").click(function() {
		//$("#list").toggle();
		//$("#hidden").toggle();
		//$("#list").toggleClass("xsd");
		$("#searchButt").removeAttr("disabled");
		getAll("xsd");
		$(".fwar").css("display", "none");
		$(".fxsd").css("display", "inline");
		$(".fwsdl").css("display", "none");
		$(".searchform [name=type]").val("xsd");
		$("#info").slideUp("slow");
		
	});
	$("#wsdl").click(function() {
		//$("#list").toggle();
		//$("#hidden").toggle();
		//$("#list").toggleClass("wsdl");
		$("#searchButt").removeAttr("disabled");
		getAll("wsdl");
		$(".fwar").css("display", "none");
		$(".fxsd").css("display", "none");
		$(".fwsdl").css("display", "inline");
		$(".searchform [name=type]").val("wsdl");
		$("#info").slideUp("slow");
	});
	
	$("#hidden").click(function() {
		if(!($("#searchButt").is(":disabled")))
		{
			if ($("#search").is(":hidden")) {
	            $("#search").slideDown("slow");
	        } else {
	            $("#search").slideUp("slow");
	        }
		}
			//$("#search").toggle();
	});
	
	function getAll (type) {
		$.ajax({
			url: "getAll.html",
			data: (type),
			contentType: "application/text; charset=utf-8",
			type: "POST",
			dataType: "text",
			success: function(data) {
				var all = $.parseJSON(data);
				//console.log(data); 
				printTable(all, type);
				
			}
		});
	}
	
	$("#searchformId").submit(function(event) {
		$("#info").slideUp("slow");
		var oElements = {};
		$(".searchform [name]").each(function(){
		    oElements[this.name] = this.value;
		});
		var type = oElements.type;
		var searchdata = $.toJSON(oElements);
		$.ajax({
			url: "search.html",
			data: (searchdata),
			contentType: "application/text; charset=utf-8",
			type: "POST",
			dataType: "text",
			success: function(data) {
				var found = $.parseJSON(data);
				//console.log(data); 
				printTable(found, type);
				
			}
		});
		return false;
	});

	//-----------------Print list of uploaded files----------------
	function printTable (data, type) {
		$("#list").css("display", "block").html("<table id=\"listview\" class=\"list\"><tr><td>Name</td><td>Version</td>");
		for(var i = 0; i < data.length; i++) {
			$("#listview tr:last").after("<tr id=\""+i+"\" class=\"detail\"><td><img src='resources/images/information.png' alt='Zobrazit podrobnosti' /> "+data[i].name+"</td><td>"+data[i].version+"</td>");
			
		}
		$(".detail").click(function() {
			var num = parseInt($(this).attr("id"));
			//$("#info").css("display", "block")
			//$("#info").slideDown("slow")
			var paging = "<strong>Versions:</strong><br />";
			for(i=1;i<=data[num].version;i++) {
				paging += "<span class='number";
				if(i==data[num].version)
					paging += " activeVersion";
				paging += "'>"+i+"</span>";
			}
			$("#paging").html(paging);
			//var type = $("#list").attr("class");
			$("#paging span").click(function() {
				$("#paging span.activeVersion").toggleClass("activeVersion");
				$(this).toggleClass("activeVersion");
				console.log(parseInt($(this).html()));
				var ajaxData = $.toJSON({type: type, id: num+1, version: parseInt($(this).html())});
				$.ajax({
					url: "getVersions.html",
					data: (ajaxData),
					contentType: "application/text; charset=utf-8",
					type: "POST",
					dataType: "text",
					success: function(data) {
						var artifact = $.parseJSON(data);
						//console.log(data); 
						printArtifact(artifact, type);
						
					}
				});
			});
			printArtifact(data[num], type);
			
			$("#info").slideDown("slow");
		});
	}
	
	function printArtifact(data, type) {
		$("#versionInfo").html("<strong>ID:</strong> "+data.id+"</br>"+"<strong>Name:</strong> "+data.name+"</br><strong>Version:</strong> "+data.version+"</br>"+"<strong>File Name:</strong> <a href='download/"+data.fileName+"/"+data.name+"'><img src='resources/images/disk.png' alt='Download file' /> Download</a></br><hr/>");
		if(type == "war") {
			/*$("#versionInfo").append("<table id=\"listeners\" class='list'><tr>Listeners</tr></table></br><table id=\"filters\"><tr>Filters</tr></table></br>");
			for(var l = 0; l < data.listeners.length; l++) {
				$("#listeners tr:last").after("<tr><td>"+data.listeners[l]+"</td></tr>");
			}
			for(var f = 0; f < data.filters.length; f++) {
				$("#filters tr:last").after("<tr><td>"+data.filters[f]+"</td></tr>");
			}*/

			$("#versionInfo").append("<strong>Listeners</strong><br/><ul id=\"listeners\"></ul></br><strong>Filters</strong><br/><ul id=\"filters\"></ul><br/>");
			for(var l = 0; l < data.listeners.length; l++) {
				$("#listeners").append("<li>"+data.listeners[l]+"</li>");
			}
			for(var f = 0; f < data.filters.length; f++) {
				$("#filters").append("<li>"+data.filters[f]+"</li>");
			}
		}
		else if(type == "xsd") {
			$("#versionInfo").append("<strong>Simple Types</strong><br/><ul id=\"simpleTypes\"></ul></br><strong>Complex Types</strong><br/><ul id=\"complexTypes\"></ul></br><strong>Attributes</strong><br/><ul id=\"attributes\"></ul></br><strong>Elements</strong><br/><ul id=\"elements\"></ul></br>");
			for(var st = 0; st < data.simpleTypes.length; st++) {
				$("#simpleTypes").append("<li>"+data.simpleTypes[st]+"</li>");
			}
			for(var ct = 0; ct < data.complexTypes.length; ct++) {
				$("#complexTypes").append("<li>"+data.complexTypes[ct]+"</li>");
			}
			for(var a = 0; a < data.attributes.length; a++) {
				$("#attributes").append("<li>"+data.attributes[a]+"</li>");
			}
			for(var e = 0; e < data.elements.length; e++) {
				$("#elements ").append("<li>"+data.elements[e]+"</li>");
			}
		}
		else {
			$("#versionInfo").append("<strong>Details:</strong><br/><table class='list' id=\"operations\"><tr><th>Name</th><th>Request</th><th>Response</th></tr></table></br>");
			for(var o = 0; o < data.operations.length; o++) {
				$("#operations tr:last").after("<tr><td>"+data.operations[o].name+"</td>"+"<td>"+data.operations[o].request+"</td>"+"<td>"+data.operations[o].response+"</td></tr>");
			}
		}
	}

	var result = decodeURI((RegExp('type=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
	if(result == "war")
		$("#war").click();
	else if(result == "xsd")
		$("#xsd").click();
	else if(result == "wsd")
		$("#wsdl").click();
	
});	
	
	
</script>
</body>
</html>
